<?php


namespace app\community\controller;


use app\api\controller\Common;
use app\common\controller\Together;
use app\index\controller\Base;
use think\Config;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Nav extends Base
{

    //导航分类
    public function index(){
        $name = $propertyName = $status = "";
        //获取景区列表
        $Together = new Together();
        $community = $Together->findColony();
        $communityWhere = array_column($community,"id");
        $where['a.colony_id'] = ['in',$communityWhere];
        if(!empty($_GET['name'])){
            $name = $_GET['name'];
            $where['a.name'] = ['like',"%{$name}%"];
        }
        if(!empty($_GET['propertyName'])){
            $propertyName = $_GET['propertyName'];
            $where['b.name'] = ['like',"%{$propertyName}%"];
        }
        if(!empty($_GET['status'])){
            $status = $_GET['status'];
            $where['a.status'] = $status;
        }
        $thisWechat = Session::get("thisWechat");
        $where['a.uniacid'] = $thisWechat['id'];
        $type_val = Config::get("model");

        if(!empty($_GET['type'])){
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $list = Db::name("community_nav")->alias("a")
                    ->join("community_colony b","a.colony_id = b.id")
                    ->field("a.*,b.name b_name")->order("a.sort desc,a.id desc")->limit($start,$limit)
                    ->where($where)->select();
                foreach ($list as $k=>$v){
                    $v['logo'] = empty($v['logo']) ? "/static/images/nopic-107.png" : $v['logo'];
                    $list[$k]['logo'] = "<img src='".$v['logo']."' style='max-height:50px;max-width:50px;'>";
                    $list[$k]['type'] = $type_val[$v['type']];
                }
                $count = Db::name("community_nav")->alias("a")
                    ->join("community_colony b","a.colony_id = b.id")
                    ->where($where)->count();
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            }catch (Exception $e){
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }

        $assign = [
            "name"=>$name,
            "propertyName"=>$propertyName,
            "status"=>$status
        ];
        $this->assign($assign);
        return $this->fetch('index');
    }

    //添加导航
    public function addNav(){
        $Together = new Together();
        $community = $Together->findColony();
        $type = Config::get("model");
        $assign = [
            'community'=>$community,
            'type'=>$type
        ];
        $this->assign($assign);
        if(empty($_POST['id'])){
            return view("addNav");
        }else{
            $detail = Db::name("community_nav")->where(['id'=>$_POST['id']])->find();
            $this->assign("detail",$detail);
            return view("editNav");
        }
    }

    //执行操作
    public function doNav(){
        try{
            $common = new Common();
            $rule = [
                ["colony_id", "require", "请选择景区"],
                ["name", "require", "请填写导航名称"],
                ["logo", "require", "请上传导航图标"],
            ];
            $validate = new Validate($rule);
            $value = $this->request->post();
            $thisWechat = Session::get("thisWechat");
            $value['uniacid'] = $thisWechat['id'];
            $value['sort'] = intval($value['sort']);
            if (!$validate->check($value)) {
                throw new Exception($validate->getError());
            }
            //查询导航是否已存在
            if(!empty($value['id'])){
                $where['id'] = ['neq',$value['id']];
            }
            $where['name'] = $value['name'];
            $where['colony_id'] = $value['colony_id'];
            $device = Db::name("community_nav")->where($where)->value("*");
            if(!empty($device)){
                throw new Exception("该导航已存在，请勿重复添加");
            }
            //获取景区名称
            $propertyName = Db::name("community_colony")->where(['id'=>$value['colony_id']])->value("name");
            if(empty($value['id'])){
                $value['addtime'] = time();
                $value['add_id'] = Session::get("id");
                $navid = Db::name("community_nav")->insertGetId($value);
                $log['remark'] = "账户【" . Session::get("user_name") . "】为景区【".$propertyName."】添加导航【".$value['name']."】,导航id【".$navid."】" ;
            }else{
                $value['updatetime'] = time();
                $value['update_id'] = Session::get("id");
                Db::name("community_nav")->where(['id'=>$value['id']])->update($value);
                $log['remark'] = "账户【" . Session::get("user_name") . "】修改景区区【".$propertyName."】导航【".$value['name']."】,导航id【".$value['id']."】" ;
            }
            $log['controller'] = "Nav";
            $log['methods'] = "doNav";
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //删除导航
    public function delNav(){
        Db::startTrans();
        try{
            $id = $_POST['id'];
            $navList = Db::name("community_nav")->where(['id'=>['in',$id]])->field("colony_id,name")->select();

            $name = "";
            $common = new Common();
            foreach ($navList as $k=>$v){
                //获取小区名称
                $propertyName = Db::name("community_colony")->where(['id'=>$v['colony_id']])->value("name");
                $log['remark'] = "管理员【" . Session::get("user_name") . "】删除景区【".$propertyName."】导航【".$v['name']."】";
                $log['controller'] = "Nav";
                $log['methods'] = "delNav";
                $common->addLog($log);
            }
            Db::name("community_nav")->where(['id'=>['in',$id]])->delete();
            Db::commit();
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;

    }

}